{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a517affc-fdef-4627-b1f5-5c4754f44c14",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import numpy as np\n",
    "\n",
    "out_path = '/home/ljw22/workspace/qwen/MUSER-main/predicate_top100.json'\n",
    "# 训练集和数据集合并，因为不需要进行训练，全部进行测试\n",
    "train_test_path = '/home/ljw22/workspace/qwen/MUSER-main/data/cases/train_test.json'\n",
    "train_test_file = open(train_test_path, 'r')\n",
    "train_test = json.load(train_test_file)\n",
    "test_querys = train_test['train'] + train_test['test']  #\n",
    "\n",
    "\n",
    "qc_pairs_path = '/home/ljw22/workspace/qwen/MUSER-main/data/cases/cands_by_query.json'\n",
    "qc_pairs_file = open(qc_pairs_path, 'r')\n",
    "qc_pairs = json.load(qc_pairs_file)\n",
    "\n",
    "feature_path = \"/home/ljw22/workspace/qwen/MUSER-main/data/features/14B_feature.json\"\n",
    "\n",
    "with open(feature_path) as fin:\n",
    "    feature = json.load(fin)\n",
    "\n",
    "docid_list = list(feature.keys())\n",
    "\n",
    "docid_id_dict = {}\n",
    "\n",
    "for id, docid in enumerate(docid_list):\n",
    "    docid_id_dict[docid] = id\n",
    "\n",
    "\n",
    "feature_matric = []\n",
    "for doc in docid_list:\n",
    "    feature_matric.append(feature[doc])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d062d48a-8c61-41c4-8bd7-9122ae94db1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "计算余弦相似度\n",
      "[[1.         0.6761234  0.         ... 0.51639778 0.63245553 0.89442719]\n",
      " [0.6761234  1.         0.         ... 0.43643578 0.53452248 0.75592895]\n",
      " [0.         0.         0.         ... 0.         0.         0.        ]\n",
      " ...\n",
      " [0.51639778 0.43643578 0.         ... 1.         0.81649658 0.57735027]\n",
      " [0.63245553 0.53452248 0.         ... 0.81649658 1.         0.70710678]\n",
      " [0.89442719 0.75592895 0.         ... 0.57735027 0.70710678 1.        ]]\n"
     ]
    }
   ],
   "source": [
    "A = np.array(feature_matric)\n",
    "\n",
    "print(\"计算余弦相似度\")\n",
    "# 计算每一行的 L2 范数（每个特征的长度）\n",
    "norms = np.linalg.norm(A, axis=1, keepdims=True)\n",
    "\n",
    "# 计算余弦相似度矩阵\n",
    "norms[norms == 0] = 1  # 可以把零范数的行设为1，防止除以0\n",
    "\n",
    "# 计算余弦相似度矩阵\n",
    "similarity_matrix = np.dot(A, A.T) / (norms * norms.T)\n",
    "\n",
    "print(similarity_matrix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec4b659b-b838-42f8-a605-fcdc7a165cf8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "确定候选\n"
     ]
    }
   ],
   "source": [
    "sorted_ndarray = np.argsort(similarity_matrix, axis=1)\n",
    "sorted_array = sorted_ndarray.tolist()\n",
    "\n",
    "predicate_top100 = {}\n",
    "\n",
    "print(\"确定候选\")\n",
    "for idx, cand in enumerate(sorted_array):\n",
    "    cand = reversed(cand)\n",
    "    docid = docid_list[idx]\n",
    "    if docid not in qc_pairs.keys():\n",
    "        continue\n",
    "    \n",
    "    cand_id_list = []\n",
    "    for c in cand:\n",
    "        cid = str(docid_list[c])\n",
    "        if int(cid) in qc_pairs[docid]:\n",
    "            cand_id_list.append(cid)\n",
    "\n",
    "    \n",
    "    \n",
    "    assert len(cand_id_list) == 100\n",
    "    predicate_top100[docid] = cand_id_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8297ec17-f674-4db5-808d-76040b9f8449",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "out_file = open(out_path, 'w')\n",
    "json.dump(predicate_top100, out_file)\n",
    "out_file.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d2c589a-0354-42bd-8e76-2e7c58b73ee9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5fa432c-5c22-45a7-a803-042460248da1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
